﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iOak.Domain.Campaigns;
using iOak.Business.Campaigns;
using iOak.Common.Untity;
using iOak.Common;

namespace iOak.Web.Administration
{
    public partial class CampaignDetails : BasePage
    {
        private const string CampaignSessionName = "CampaignDetails_CampaignItem";
        private WorkAssistant _workassistant = new WorkAssistant();

        private Campaign CampaignItem
        {
            get
            {
                Campaign campaign = null;
                if (Session[CampaignSessionName] != null)
                {
                    campaign = Session[CampaignSessionName] as Campaign;
                }
                return campaign;
            }
            set
            {
                Session[CampaignSessionName] = value;
            }
        }

        /// <summary>
        /// 页面模式0=新建;1=编辑;
        /// </summary>
        private int PageMode
        {
            get
            {
                int pageMode = 0;
                if (this.ViewState["PageMode"] != null)
                {
                    pageMode = int.Parse(this.ViewState["PageMode"].ToString());
                }
                return pageMode;
            }
            set
            {
                this.ViewState["PageMode"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                txtStartDate.Attributes.Add("Readonly", "Readonly");
                txtEndDate.Attributes.Add("Readonly", "Readonly");
                IniPolicyList();
                if (Request.QueryString["CampaignId"] != null)
                {
                    CampaignService campaignService = new CampaignService();
                    Guid campaignId = Guid.Parse(Request.QueryString["CampaignId"]);
                    CampaignItem = campaignService.Retrieve(campaignId);
                    PageMode = 1;
                    IniData();
                }
                else
                {
                    CampaignItem = new Campaign();
                    CampaignItem.CampaignId = Guid.NewGuid();
                    PageMode = 0;
                }
            }
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            CampaignItem.Subject = txtSubject.Text.Trim();
            CampaignItem.Description = txtDescription.Text.Trim();
            CampaignItem.StartDate = DateTime.Parse(txtStartDate.Text.Trim());
            CampaignItem.EndDate = DateTime.Parse(txtEndDate.Text);
            CampaignItem.Status = chkOpen.Checked ? 1 : 0;
            CampaignItem.PolicyId = Guid.Parse(ddlPolicy.SelectedValue);
            CampaignItem.Bounty = Decimal.Parse(txtBounty.Text);

            CampaignService campaignService = new CampaignService();
            CampaignItem.ModifiedBy = _workassistant.CurrentUser.UserId;
            CampaignItem.ModifiedOn = DateTime.Now;
            CampaignItem.SelectedAttaches = txtSelectedAttaches.Text.Replace(";", "").Replace("\n", ";");
            if (PageMode == 0) 
            {
                CampaignItem.CreatedBy = _workassistant.CurrentUser.UserId;
                CampaignItem.CreatedOn = DateTime.Now;
                campaignService.Create(CampaignItem);
            }
            else if (PageMode == 1) 
            {
                campaignService.Update(CampaignItem);
            }
            Response.Redirect("/Administration/CampaignList.aspx");
        }

        private void IniPolicyList()
        {
            PolicyService policyService = new PolicyService();
            ddlPolicy.DataSource = policyService.LoadAll();
            ddlPolicy.DataTextField = "Subject";
            ddlPolicy.DataValueField = "PolicyId";
            ddlPolicy.DataBind();
        }

        private void IniData()
        {
            txtSubject.Text = CampaignItem.Subject;
            txtDescription.Text = CampaignItem.Description;
            txtStartDate.Text = CampaignItem.StartDate.ToString("yyyy-MM-dd");
            txtEndDate.Text = CampaignItem.EndDate.ToString("yyyy-MM-dd");
            chkOpen.Checked = CampaignItem.Status != (int)CampaignStatus.Open;
            ddlPolicy.SelectedValue = CampaignItem.PolicyId.ToString();
            txtBounty.Text = Convert.ToInt32(CampaignItem.Bounty).ToString();
            txtSelectedAttaches.Text = CampaignItem.SelectedAttaches.Replace(";", "\n");
        }
    }
}